﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace ImportC_Dll
{
    class Program
    {
        /// <summary>
        /// 这个接口用于测试数据类型的长度
        /// </summary>
        [DllImport("Jaunt_Card.dll")]
        public static extern void PrintDataLen();
        /// <summary>
        /// 计算卡密码
        /// </summary>
        /// <param name="serialNum"></param>
        /// <param name="currPassword"></param>
        /// <returns></returns>
        [DllImport("Jaunt_Card.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern UInt64 CalcCardPassword(uint serialNum, uint currPassword);
        /// <summary>
        /// CRC1校验
        /// </summary>
        /// <param name="CardBuf"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        [DllImport("Jaunt_Card.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern byte FM1701_CRC1([MarshalAs(UnmanagedType.LPArray)] byte[] CardBuf, byte length);
        /// <summary>
        /// CRC2校验
        /// </summary>
        /// <param name="CardBuf"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        [DllImport("Jaunt_Card.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern byte FM1701_CRC2([MarshalAs(UnmanagedType.LPArray)] byte[] CardBuf, byte length);

        static void Main(string[] args)
        {
            PrintDataLen();
            byte[] buf =  { 0x01,0x02,0x03,0x04,0x05};
            byte b = FM1701_CRC1(buf, (byte)buf.Length);
            Console.WriteLine("CRC1 result=" + b.ToString("X2"));
            b = FM1701_CRC2(buf, (byte)buf.Length);
            Console.WriteLine("CRC2 result=" + b.ToString("X2"));

            //卡密码计算
            uint cardSerialNum = 556879121;
            uint currCardPassword = 123456;

            //根据卡序列号与当前卡密码计算得出要写入卡中的密码
            uint writePassword = (uint)CalcCardPassword(cardSerialNum, currCardPassword);

            Console.WriteLine("CalcCardPassword result=" + writePassword.ToString("X8"));

            Console.ReadKey();
        }
    }
}
